package hydrap2p.search;

import java.util.List;

import hydrap2p.ServiceLocation;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class ListenerThrift implements ListenerIface {

	private SearchListener.Client connection;
	private ServiceLocation location;
	
	public ListenerThrift(ServiceLocation location) throws TException
	{
		if (location==null)
			throw new TException("Invalid service location");
		
		this.location=location;

		TTransport transport = new TSocket(location.address,location.port);
		transport.open();
		connection=new SearchListener.Client(new TBinaryProtocol(transport));
		
		if (!connection.supportedInterfaces().contains("SearchListener"))
			throw new TException("Module does not implement SearchListener");
	}
	public ServiceLocation getLocation()
	{
		return location;
	}
	
	@Override
	public void addSearchResult(int searchid, SearchResult result)
			throws Exception {
		connection.addSearchResult(searchid, result);
	}

	@Override
	public void delSearch(int searchid) throws Exception {
		connection.delSearch(searchid);
	}

	@Override
	public void newSearch(int searchid, SearchQuery query, List<String> providers) throws Exception {
		connection.newSearch(searchid, query, providers);
	}

	@Override
	public void searchFinished(int searchid) throws Exception {
		connection.searchFinished(searchid);
	}

	@Override
	public void searchProviderStarted(int searchid, String name)
			throws Exception {
		connection.searchProviderStarted(searchid, name);
	}
	
	@Override
	public void searchProviderFinished(int searchid, String name)
			throws Exception {
		connection.searchProviderFinished(searchid, name);
	}

}
